package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysGys;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsOrderRepair;
import com.jy.wms.pojo.WmsOrderRepairDetailed;
import com.jy.wms.service.WmsOrderRepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsOrderRepairController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Mon Nov 13 09:20:55 GMT+08:00 2017
 */


@RestController
@RequestMapping("/wmsorderrepair")
@Api(value = "/wmsorderrepair", description = "关于订单驱动补货操作")
public class  WmsOrderRepairController extends BaseController<WmsOrderRepairService,WmsOrderRepair> {

    @PostMapping(value ="list")
    @ApiOperation(value = "查询信息",response = JSONObject.class, notes = "查询信息")
    public @ResponseBody
    ResultPage<WmsOrderRepair> getWmsOrderRepair(@RequestBody WmsOrderRepair entity, HttpSession session) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        Page<WmsOrderRepair> list= baseService.baseSelectPage(entity,ckid);
            return renderPage(list);
    }

    @PostMapping(value ="add")
    @ApiOperation(value = "生成补货单",response = JSONObject.class, notes = "生成补货单")
    public @ResponseBody JSONObject add (@RequestBody SetAndModifyModel setModel, HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        List<SysGys> gys = (List) session.getAttribute(BaseSessionInfo.SessionGys);//供应商
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        try {
            baseService.save(setModel.getIds(),hzid,ckid,userinfo.getUser().getName());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "补货单生成成功", "", "");
        }  catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "补货单生成成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "补货单生成失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "补货单生成失败", e.getMessage(), "");
        }
    }
    @ApiOperation(value = "删除补货单", response = JSONObject.class, notes = "补货单删除，ids:选中的多条补货单的id集合")
    @PostMapping(value = "delete")

    public @ResponseBody
    JSONObject deleteOrder(@RequestBody SetAndModifyModel setModel, HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        try {
            baseService.deleteOrder(setModel.getIds(), userinfo.getUser().getName());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "补货单删除成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "补货单删除成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "补货单删除失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "采购订单删除失败", e.getMessage(), "");
        }
    }

    @ApiOperation(value = "确认补货单", response = JSONObject.class, notes = "补货单确认，ids:选中的多条补货单的id集合")
    @PostMapping(value = "confirmOrder")
    public @ResponseBody
    JSONObject confirmOrder(@RequestBody SetAndModifyModel setModel, HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        List<SysGys> gys = (List) session.getAttribute(BaseSessionInfo.SessionGys);//供应商
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        try {
            baseService.confirmOrder(setModel.getIds(), userinfo.getUser().getName(),hzid);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "补货单确认成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "补货单确认成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "补货单确认失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "采购订单确认失败", e.getMessage(), "");
        }
    }











    @ApiOperation(value = "导出订单驱动补货", response = JSONObject.class, notes = "导出订单驱动补货")
    @PostMapping(value = "getOutPutList")
    public JSONObject getOutPutList(HttpServletResponse response, @RequestBody Map map, HttpSession session) throws IOException {
        String cjsj_start = (String) map.get("cjsj_start");
        String cjsj_end = (String) map.get("cjsj_end");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String start = null;
        String end = null;
        if (cjsj_start != null && cjsj_start != "") {
            long scsj_start = Long.parseLong(cjsj_start);//开始时间
            Date date1 = new Date(scsj_start);
            start = format.format(date1);
        }
        if (cjsj_end != null && cjsj_end != "") {
            long scsj_end = Long.parseLong(cjsj_end);//结束时间
            Date date2 = new Date(scsj_end);
            end = format.format(date2);
        }
        map.put("cjsj_start", start);
        map.put("cjsj_end", end);
        String filename = "订单驱动补货单";
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        String ckmc = userinfo.getUser().getOrgName();//仓库名称
        map.put("ckid",ckid);
        List<String> ids = (List) map.get("ids");
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("订单驱动补货表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams("补货单","补货单");
        ep.setStyle(ManySheetOneSyler.class);
        //List<WmsOrderRepair> list = baseService.selectBatchIds(ids);
        List<WmsOrderRepairDetailed> list = baseService.getOutPutList(ids,map);

        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsOrderRepairDetailed.class, list);
        workbook.write(response.getOutputStream());
        return null;
    }


    @ApiOperation(value = "打印补货单", response = JSONObject.class, notes = "打印补货单,ids:补货单id")
    @PostMapping(value = "print")
    public @ResponseBody
    JSONObject print(@RequestBody SetAndModifyModel setModel) {
        JSONObject jsonObject = null;
        try {
            jsonObject = baseService.print(setModel.getIds());
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }


    @ApiOperation(value = "编辑时查找明细集合", response = JSONObject.class, notes = "编辑时查找明细集合,id:补货单id")
    @PostMapping(value = "getDetailList")
    public @ResponseBody
    JSONArray getDetailList(@RequestBody SetAndModifyModel setModel) {
        JSONArray jsonArray = null;
        try {
            jsonArray = baseService.getDetailList(setModel.getId());
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    @ApiOperation(value = "下发补货任务", response = JSONObject.class, notes = "下发补货任务，id:选中的补货单的id")
    @PostMapping(value = "addBhRw")
    public @ResponseBody
    JSONObject addBhRw(@RequestBody Map map , HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        try {
            baseService.addBhRw((String)map.get("id"),hzid,(String)map.get("userId"),(String)map.get("userName"),ckid);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "下发订单补货任务成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "下发订单补货任务成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "下发订单补货任务失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "下发订单补货任务失败", e.getMessage(), "");
        }
    }



}
